import {ORDER_LIST,REFRESH_LIST,LOGISTICS_LIST} from './constants';

const initState = {
    orderList2:[],
    orderList3:[],
    orderList7:[],
    orderList6:[],
    orderList10:[],
    logisticsName: '',
    logisticsTel: '',
    logisticsNumber: '',
    logisticsList: [],
    hasMore:false,
}

const getOrderList = (orderListModal,oldOrderModal) => {
  var orderList = {};
  var ol = [];
  var hasMore = false;
  var size = 10;

  if (orderListModal.orderList.length < size){
    hasMore = false;
  }else{
    hasMore = true;
  }
  
  if (orderListModal.orderState == 2){
    if (oldOrderModal != undefined){
      var list = oldOrderModal.orderList2;
      ol = list.concat(orderListModal.orderList)
    }else{
      ol = orderListModal.orderList;
    }
    orderList = {orderList2:ol};
  }else if(orderListModal.orderState == 3){
    if (oldOrderModal != undefined){
      var list = oldOrderModal.orderList3;
      ol = list.concat(orderListModal.orderList)
    }else{
      ol = orderListModal.orderList;
    }
    orderList = {orderList3:ol};
  }else if(orderListModal.orderState == 7){
     if (oldOrderModal != undefined){
      var list = oldOrderModal.orderList7;
      ol = list.concat(orderListModal.orderList)
    }else{
      ol = orderListModal.orderList;
    }
    orderList = {orderList7:ol};
  }else if(orderListModal.orderState == 6){
    if (oldOrderModal != undefined){
      var list = oldOrderModal.orderList6;
      ol = list.concat(orderListModal.orderList)
    }else{
      ol = orderListModal.orderList;
    }
    orderList = {orderList6:ol};
  }else if(orderListModal.orderState == 10){
    if (oldOrderModal != undefined){
      var list = oldOrderModal.orderList10;
      ol = list.concat(orderListModal.orderList)
    }else{
      ol = orderListModal.orderList;
    }
    orderList = {orderList10:ol};
  }
  
  orderList.hasMore = hasMore;

  return orderList;
}

const reducer = (state = initState, action) => {
  switch (action.type) {
    case ORDER_LIST:
        var orderList = getOrderList(action.payload,state);
        return Object.assign({},state,orderList);
    case REFRESH_LIST:
        var orderList = getOrderList(action.payload);
        return Object.assign({},state,orderList);
    case LOGISTICS_LIST:
        return Object.assign({}, state, action.payload);
    default:
        return state;
  }
};

export default reducer;
